﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;

namespace MODEL
{
    public class ProcessItem : ViewModelBase
    {

        public string Title { get; set; }

        public DateTime BeginTime { get; set; }

        public TimeSpan ExcuteTime { get; set; }

        private DateTime? m_CurrentTime = null;
        private DateTime CurrentTime => this.m_CurrentTime.HasValue ? this.m_CurrentTime.Value : DateTime.Now;
        public bool IsOpen { get; set; }
      
        public ProcessStatusEnum ProcessStatus
        {
            get
            {
                if (!IsOpen)
                {
                    return ProcessStatusEnum.None;
                }
                else if (CurrentTime < BeginTime)
                {
                    return ProcessStatusEnum.Wait;
                }
                else if (CurrentTime < BeginTime + ExcuteTime)
                {
                    return ProcessStatusEnum.InProcess;
                }
                else
                {
                    return ProcessStatusEnum.None;
                }
            }
        }
      
        public int TotalSecends => (int)Math.Ceiling(ExcuteTime.TotalSeconds);

        public int SpendSecends
        {
            get
            {
                switch (ProcessStatus)
                {
                    case ProcessStatusEnum.Wait:
                        return 0;
                    case ProcessStatusEnum.InProcess:
                        return (int)Math.Floor((this.CurrentTime - this.BeginTime).Duration().TotalSeconds);
                    default:
                        return TotalSecends;
                }
            }
        }

        public string OpenStatusMessage => this.ProcessStatus == ProcessStatusEnum.InProcess ? "状态: 开" : "状态: 关";

        public string RemainingMessage
        {
            get
            {
                switch (this.ProcessStatus)
                {
                    case ProcessStatusEnum.Wait:
                        return "未进行";
                    case ProcessStatusEnum.InProcess:
                        TimeSpan remainingTime = (this.BeginTime.Add(ExcuteTime) - this.CurrentTime).Duration();
                        if (remainingTime.TotalHours > 1)
                        {
                            return $"还剩{remainingTime.Hours}小时{remainingTime.Minutes}分钟";
                        }
                        else if (remainingTime.TotalMinutes > 1)
                        {
                            return $"还剩{remainingTime.Minutes}分钟{remainingTime.Seconds}秒";
                        }
                        else
                        {
                            return $"还剩{remainingTime.Seconds}秒";
                        }
                    default:
                        return "已完成";
                }
            }
        }

        public void RaiseUIProperty()
        {
            this.m_CurrentTime = DateTime.Now;
            base.RaisePropertyChanged(() => TotalSecends);
            base.RaisePropertyChanged(() => SpendSecends);
            base.RaisePropertyChanged(() => RemainingMessage);
        }
    }
}
